using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;

namespace Home.Controls
{
    internal class TreeLineMaker
    {
        #region Properties

        private int m_Depth;
        public int Depth
        {
            get { return m_Depth; }
            set { m_Depth = value; }
        }

        private bool m_IsFirstNode = true;
        public bool IsFirstNode
        {
            get { return m_IsFirstNode; }
            set { m_IsFirstNode = value; }
        }

        private bool m_IsLastNode = true;
        public bool IsLastNode
        {
            get { return m_IsLastNode; }
            set { m_IsLastNode = value; }
        }

        private bool m_IsShowLines = true;
        public bool IsShowLines
        {
            get { return m_IsShowLines; }
            set { m_IsShowLines = value; }
        }

        private bool? m_IsExpand = null;
        public bool? IsExpand
        {
            get { return m_IsExpand; }
            set { m_IsExpand = value; }
        }

        private bool m_HasChild = true;
        public bool HasChild
        {
            get { return m_HasChild; }
            set { m_HasChild = value; }
        }

        private int m_ChildNumber;
        public int ChildNumber
        {
            get { return m_ChildNumber; }
            set { m_ChildNumber = value; }
        }

        private string m_ImageUrl;
        public string ImageUrl
        {
            get { return m_ImageUrl; }
            set { m_ImageUrl = value; }
        }

        private string m_Text;
        public string Text
        {
            get { return m_Text; }
            set { m_Text = value; }
        }

        private string m_ImageHorizontal;
        public string ImageHorizontal
        {
            get { return m_ImageHorizontal; }
            set { m_ImageHorizontal = value; }
        }

        private string m_ImageVertial;
        public string ImageVertial
        {
            get { return m_ImageVertial; }
            set { m_ImageVertial = value; }
        }

        private string m_ImageExpand;
        public string ImageExpand
        {
            get { return m_ImageExpand; }
            set { m_ImageExpand = value; }
        }

        private string m_ImageCollapse;
        public string ImageCollapse
        {
            get { return m_ImageCollapse; }
            set { m_ImageCollapse = value; }
        }

        private bool m_IsParentLastNode = true;
        public bool IsParentLastNode
        {
            get { return m_IsParentLastNode; }
            set { m_IsParentLastNode = value; }
        }

        private TreeListNode m_TreeListNode = null;
        internal TreeListNode TreeListNode
        {
            get { return m_TreeListNode; }
            set { m_TreeListNode = value; }
        }
        #endregion

        internal TreeLineMaker()
        {

        }

        internal string Make()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<table height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
            sb.Append("<tr>");
            for (int i = 0; i < Depth; i++)
            {
                sb.AppendFormat("<td style=\"height:35%; {0}\"></td>", GetTopLine(i));
                sb.Append("<td></td>");
            }
            if (HasChild)
            {
                sb.AppendFormat("<td style=\"height:35%; {0}\"></td>", GetImageTopLine());
                sb.Append("<td></td>");
            }
            else
            {
                sb.AppendFormat("<td style=\"height:35%; {0}\"></td>", GetTopLine(Depth));
                sb.Append("<td></td>");
            }

            if (this.ImageUrl != string.Empty)
            {
                sb.AppendFormat("<td rowspan=\"2\" valign=\"bottom\"><img src=\"{0}\" title=\"{1}\"></td>", ImageUrl, TreeListNode.ImageToolTip);
                
            }
            sb.AppendFormat("<td rowspan=\"3\" title=\"{1}\">&nbsp;{0}</td", this.Text,TreeListNode.ToolTip);

            sb.Append("</tr><tr style=\"height:9px;\">");
            //////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < Depth; i++)
            {
                sb.AppendFormat("<td style=\"width:5px; {0}\"></td>", GetMiddleCenterLine(i));
                sb.Append("<td style=\"width:11px;\"></td>");
            }
            if (HasChild)
            {
                sb.AppendFormat("<td><img src=\"{0}\" id=\"img_{1}\" onClick=\"TreeNodeClick(this);\" height=\"9px\" width=\"9px\" style=\"cursor:pointer;\"></td>", GetImageMiddleLine(),TreeListNode.DisplayID);
                sb.AppendFormat("<td style=\"width:7px; background-repeat:repeat-x; background-position:center; background-image:url({0})\"></td>", ImageHorizontal);

            }
            else
            {
                sb.AppendFormat("<td style=\"width:5px; {0}\"></td>", GetMiddleLine());
                sb.AppendFormat("<td style=\"width:11px; background-repeat:repeat-x; background-position:center; background-image:url({0})\"></td>", ImageHorizontal);
            }
            
            sb.Append("</tr><tr>");
            //////////////////////////////////////////////////////////////////////////
            for (int i = 0; i < Depth; i++)
            {
                sb.AppendFormat("<td style=\"height:35%; {0}\"></td>", GetBottomLine(i));
                sb.Append("<td></td>");
            }
            if (HasChild)
            {
                sb.AppendFormat("<td style=\"height:35%; {0}\"></td>", GetImageBottomLine());
                sb.Append("<td></td>");
            }
            else
            {
                sb.AppendFormat("<td style=\"height:35%; {0}\"></td>", GetBottomLine(Depth));
                sb.Append("<td></td>");
            }

            if (this.ImageUrl != string.Empty)
            {
                if (HasChild)
                {
                    sb.AppendFormat("<td style=\"height:35%;padding-left:4px; width:9px\"><span id=\"span_{1}\" style=\"height:100%; width:1px; background-position:left; background-repeat:repeat-y;background-image:url({0});{2}\"><img height='100%' width='0px'></span></td>", ImageVertial,TreeListNode.DisplayID,GetSpanStyle());
                }
                else
                {
                    sb.Append("<td></td>");
                }
            }
            sb.Append("</tr></table>");

            return sb.ToString();
        }

        private string GetTopLine(int nDepth)
        {
            string strResult=string.Format("background-position:right; background-repeat:repeat-y;background-image:url({0})", ImageVertial);
            if (nDepth==Depth)
            {
                return strResult;
            }

            TreeNode parent = TreeListNode;
            for (int i = Depth; i > nDepth; i--)
            {
                parent = parent.Parent;
            }

            if (((TreeListNode)parent).IsLastNode)
            {
                return string.Empty;
            }
            else
            {
                return strResult;
            }                        
        }
        
        private string GetImageTopLine()
        {
            if (IsFirstNode)
            {
                return string.Empty;
            }
            else
            {
                return string.Format("background-position:center; background-repeat:repeat-y;background-image:url({0})", ImageVertial);
            }

        }

        private string GetBottomLine(int nDepth)
        {
            string strResult = string.Format("background-position:right; background-repeat:repeat-y;background-image:url({0})", ImageVertial);
            if (TreeListNode.ID == TreeListNode.TreeListView.TotalNodeNumber - 1)
            {
                return string.Empty;
            }
            //if (IsLastNode)
            //{
            //    return string.Empty;
            //}
            TreeNode parent = TreeListNode;
            for (int i = Depth; i > nDepth;i-- )
            {
                parent = parent.Parent;
            }

            if (((TreeListNode)parent).IsLastNode)
            {
                return string.Empty;
            }            
            else
            {
                return strResult;
            }
        }

        private string GetImageBottomLine()
        {
            if (IsLastNode || !HasChild)
            {
                return string.Empty;
            }
            else
            {
                return string.Format("background-position:center; background-repeat:repeat-y;background-image:url({0})", ImageVertial);
            }
        }

        private string GetMiddleLine()
        {
            if (IsLastNode)
            {
                return string.Format("background-position:right top; background-repeat:no-repeat;background-image:url({0})", ImageVertial);
            }
            else
            {               
                return string.Format("background-position:right; background-repeat:repeat-y;background-image:url({0})", ImageVertial);
            }
        }

        private string GetImageMiddleLine()
        {
            if (IsExpand==null)
            {
                if (this.Depth>=TreeListNode.TreeListView.ExpandDepth)
                {
                    return ImageExpand;
                }
                else
                {
                    return ImageCollapse;
                }
            }
            if (IsExpand.Value)
            {
                return ImageCollapse;
            }
            else
            {
                return ImageExpand;
            }
        }

        private string GetSpanStyle()
        {
            if (IsExpand == null)
            {
                if (this.Depth >= TreeListNode.TreeListView.ExpandDepth)
                {
                    return "display:none";
                }
                else
                {
                    return string.Empty;
                }
            }
            if (IsExpand.Value)
            {
                return string.Empty;
            }
            else
            {
                return "display:none";
            }
        }

        private string GetMiddleCenterLine(int nDepth)
        {
            string strResult = string.Format("background-position:right; background-repeat:repeat-y;background-image:url({0})", ImageVertial);

            TreeNode parent = TreeListNode;
            for (int i = Depth; i > nDepth; i--)
            {
                parent = parent.Parent;
            }

            if (((TreeListNode)parent).IsLastNode)
            {
                return string.Empty;
            }
            else
            {
                return strResult;
            }
        }
    }
}
